package xgenerator.ui.swing;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Toolkit;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextField;

import xgenerator.ui.swing.adapter.DataSourceAdapter;

public class DataSourceManagerDialog extends JDialog implements UIConstants {
	
	/**
	 * 组件
	 */
	private JComboBox queryComboBox;
	private JTextField queryField;
	private JButton queryBtn;
	private JButton createBtn;
	private JButton viewBtn;
	private JButton editBtn;
	private JButton deleteBtn;
	private DataSourceTable dataSourceTable;  //数据源表

	/**
	 * constructor
	 * @param owner
	 * @param title
	 * @param modal
	 */
	public DataSourceManagerDialog(Frame owner, String title, boolean modal) {
		super(owner, title, modal);
		initComponents();
	}
	
	/**
	 * <p>
	 * Title：初始化组件
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 */
	private void initComponents() {
		this.setTitle("数据源管理");
		this.setIconImage(IconUtils.createIconImage(APP_ICON_IMAGE));
		this.setContentPane(buildContentPane());

		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension screenSize = tk.getScreenSize();
		int screenHeight = screenSize.height;
		int screenWidth = screenSize.width;
		this.setSize(screenWidth / 2, screenHeight / 2);
		this.setLocation((screenWidth - getWidth()) / 2, (screenHeight - getHeight()) / 2);
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setVisible(true);
	}
	
	/**
	 * <p>
	 * Title：
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @return
	 */
	private JPanel buildNorthPane() {
		JPanel northPanel = new JPanel();
		northPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

		this.queryComboBox = new JComboBox();
		this.queryComboBox.addItem("名称");
		this.queryComboBox.addItem("类型");
		
		this.queryField = new JTextField();
		//this.queryField.setPreferredSize(new Dimension(150, 29));
		this.queryField.setPreferredSize(new Dimension(150, 21));
		this.queryBtn = new JButton("查询");
		this.createBtn = new JButton("添加");
		this.createBtn.addActionListener(new DataSourceAdapter(this));
		this.createBtn.setActionCommand(DataSourceAdapter.CREATE_DATASOURCES_COMMAND);
		this.viewBtn = new JButton("查看");
		this.editBtn = new JButton("修改");
		this.deleteBtn = new JButton("删除");
		
		northPanel.add(new JLabel("按:"));
		northPanel.add(this.queryComboBox);
		northPanel.add(this.queryField);
		northPanel.add(this.queryBtn);
		northPanel.add(new JSeparator());
		northPanel.add(this.createBtn);
		northPanel.add(this.viewBtn);
		northPanel.add(this.editBtn);
		northPanel.add(this.deleteBtn);
		
		return northPanel;
	}
	
	/**
	 * <p>
	 * Title：
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @return
	 */
	private JScrollPane buildCenterPane() {
		this.dataSourceTable = new DataSourceTable(this);
//		this.datasourceTable.setParentComponent(this);
		JScrollPane centerScrollPane = new JScrollPane(this.dataSourceTable);

		return centerScrollPane;
	}
	
	/**
	 * <p>
	 * Title：
	 * </p>
	 * @return
	 */
	private JPanel buildSouthPane() {
		JPanel northPanel = new JPanel();
		northPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

		northPanel.add(new JLabel("状态:"));
		
		return northPanel;
	}
	
	/**
	 * <p>
	 * Title：
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @return
	 */
	private JPanel buildContentPane() {
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());

		panel.add(buildNorthPane(), BorderLayout.NORTH);
		panel.add(buildCenterPane(), BorderLayout.CENTER);
		panel.add(buildSouthPane(), BorderLayout.SOUTH);

		return panel;
	}

}
